<?php

class shihuoModel{

    private $apiHost = 'http://www.shihuo.cn';
    private $javaHost = 'http://10.10.2.169:8091';


    public function getOrderInfo($orderid){
        $query = array(
            'order_id'=>$orderid,
        );

        $query = $this->makeSign($query);

        $info = common::getContents($this->apiHost.'/app_swoole_paobu/GetOrderInfo', $query, 10, 'post');
        $info = json_decode($info);
        if($info->status==0){
            return $info->data;
        }
        return null;
    }

    public function createOrder($orderinfo,$product,$address,$user){
        //$product = [array('goods_id'=>'81893','sku_id'=>'489374','number'=>1)];
        //$address = array('name'=>'张xx','postcode'=>'','province'=>'上海','city'=>'上海市','area'=>'虹口区','mobile'=>'15221053812','street'=>'花园路84号');
        $query = array(
            'order_unique_num'=>$orderinfo['order_id'],
            'total_price'=>$orderinfo['total_fee'],
            'tax_service'=>1,//0--不包税，1--包税
            'address'=>json_encode($address),
            'type'=>0,//是否参加全场或商城的活动：0--不参加活动， 1--参加活动
            'sourceChannel'=>5,
            'id_card'=>'410621198505181037',
            'ibilling_number'=>$orderinfo['order_id'],//pay_id
            'uid'=>$user['uid'],
            'user_name'=>$user['nickname'],
            'user_source'=>1,
            'products'=>json_encode($product),
        );

        $query = $this->makeSign($query);

        print_r($query);

        $info = common::getContents($this->apiHost.'/app_swoole_paobu/createOrder', $query, 10, 'post');
        $info = json_decode($info);
        if($info->status==0&&$info->data->status==0){
            return array(
                'main_order'=>$info->data->order_number,
                'child_order'=>$info->data->order_data[0]->order_id
            );
        }
        return null;
    }

    public function calculateOrderPrices($product){
        //$product = array('goods_id'=>'18087','sku_id'=>'137703','number'=>2);
        $query = array(
            'tax_service'=>1,//1--包税，0--不包税
            'source'=>5,
            'type'=>0,//是否参加全场或商城的活动：0--不参加活动， 1--参加活动
            'products'=>json_encode($product)
            );

        $query = $this->makeSign($query);

        $info = common::getContents($this->apiHost.'/app_swoole_paobu/calculateOrderPrices', $query, 10, 'post');
        $info = json_decode($info);
        if($info->status==0){
            $good_price = 0;
            foreach ($product as $v) {
                $good_price+=$v['price']*$v['number'];
            }
            //拉取最新价格
            if($info->data->goods_total_price!=$good_price){
                foreach ($product as $v) {
                    $url = $this->javaHost."/downloadhaitao?haitaoId=".$v['goods_id'];
                    common::getContents($url, $query, 30);
                }
            }

            return $info->data;
        }
        return null;

        /*total_price: 			"计算出来的总价，需要真实支付的价格",
        original_price: 		"原价，优惠前的价格",
        local_express_fee: 		"当地运费",
        national_express_fee: 	"国际运费",
        goods_total_price:    	"商品总价格",
        spare_express_fee: 		"节省运费"*/
    }

    public function updateDaigouPrice($id){
        $query = array(
            'pid'=>$id,
        );

        $query = $this->makeSign($query);

        $info = common::getContents($this->apiHost.'/app2/updateDaigouPrice', $query);
        $info = json_decode($info);
        return $info;
    }

    public function getDaigouDetail($id){
         $query = array(
             'id'=>$id,
         );

        $query = $this->makeSign($query);

        $info = common::getContents($this->apiHost.'/app2/getDaigouDetail', $query);
        $info = json_decode($info);
        $data = (array)$info->data;
        return $data;
    }

    public function getDaigouSKU($id){
        $query = array(
            'id'=>$id,
        );

        $query = $this->makeSign($query);

        $info = common::getContents($this->apiHost.'/app2/getDaigouSkuInfo', $query);
        $info = json_decode($info);
        $data = (array)$info->data->attr;

        $childGoods = array();
        $headImage = array();
        foreach ($data['content'] as $item){
            $item = (array)$item;
            $attrPathName = array();
            foreach ($data['attr'] as $attrHead){
                $attrPathName[] = $item[$attrHead];
                $headImage[$attrHead.'_'.$item[$attrHead]] = $item['img'];
            }
            $childGoods[] = array(
                'attrPathName'=>implode("—",$attrPathName),
                'attr'=>$attrPathName,
                'id'=>$item['name'],
                'stockNum'=>100,
                'saleNum'=>0,
                'saleprice'=>$item['Price'],
            );
        }

        $headAttrs = array();
        foreach ($data['attr'] as $attrHead){
            $head = array('title'=>$attrHead);
            $values =array();
            foreach ($data[$attrHead] as $attrValue){
                $key  = $attrHead.'_'.$attrValue;
                $values[] = array('title'=>$attrValue,"img_source"=>isset($headImage[$key])?$headImage[$key]:null);
            }
            $headAttrs[] = array('headInfo'=>$head,"attrValue"=>$values);
        }

        $goods = array();
        $goods['attrs'] = $headAttrs;
        $goods['childGoods'] = $childGoods;
        return $goods;
    }

    public function makeSign($array){
        $secret = '123456';

        $array['timestamp'] = time();
        $array['channel'] = 'ijoggers';
        $array['clientCode'] = '00000000';
        $array['platform'] = 'android';
        $array['v'] = '4.1.8';
        ksort($array);

        $array['token'] = md5(implode("",array_values($array)).$secret);
        return $array;
    }
}